package com.link2room.rodimus.facade.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.exception.SysException;
import com.link2room.common.jpa.condition.ConditionDetail;
import com.link2room.common.jpa.condition.L2RCondition;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.*;
import com.link2room.rodimus.constant.SignInConstant;
import com.link2room.rodimus.dto.param.base.*;
import com.link2room.rodimus.dto.param.manager.SignInRuleBaseCreateParamDto;
import com.link2room.rodimus.dto.param.manager.SignInRuleBaseFindParamDto;
import com.link2room.rodimus.dto.param.manager.SignInRuleBaseModifyParamDto;
import com.link2room.rodimus.entity.*;
import com.link2room.rodimus.facade.ISignInManagerFacadeService;
import com.link2room.rodimus.service.ISignInService;
import com.link2room.rodimus.util.CalendarUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.link2room.enums.exception.RodimusSysExceptionEnum.SIGN_IN_RULE_BASE_CREATE_ERROR;
import static com.link2room.enums.exception.RodimusSysExceptionEnum.SIGN_IN_RULE_BASE_MODIFY_ERROR;

/**
 * Created by LN on 2018/1/12
 */
@Component("signInManagerFacadeService")
public class SignInManagerFacadeServiceImpl implements ISignInManagerFacadeService {

    @Resource
    private ISignInService signInService;
    @Resource
    private ICommonService commonService;

    @Override
    public void createSignInRuleBase(SignInRuleBaseCreateParamDto paramDto) throws Exception {
        String lhotelGorupCode = paramDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGorupCode, "[lhotelGorupCode] is required.");
        SignInBaseDto signInRuleBase = paramDto.getSignInBase();
        L2RAssert.isTrue(null == signInRuleBase, new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "[signInRuleBase] is required."));


        L2RAssert.isTrue(hasNextSignInRuleBase(lhotelGorupCode), new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "已经有未生效的活动，不能新建"));
        String signInRuleBaseCode = signInRuleBase.getCode();
        if (StringUtil.isBlank(signInRuleBaseCode)) {
            signInRuleBaseCode = signInService.generateSignInRuleBaseCode(SignInConstant.CodeType.SIGNIN_RULE_BASE_CODE);
        }
        L2RAssert.isTrue(signInService.isRuleBaseExit(lhotelGorupCode, signInRuleBaseCode, null), new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "[code] is duplicate."));
        String couldReissue = signInRuleBase.getCouldReissue();
        if (StringUtil.isBlank(couldReissue)) {
            couldReissue = DefaultValue.F;
        }
        L2RAssert.isTrue(!DefaultValue.F.equals(couldReissue) && !DefaultValue.T.equals(couldReissue), new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "[couldReissue] is not invalid."));
        String description = signInRuleBase.getDescription();
        L2RAssert.stringBlank(description, "[description] is required.");
        Date startMonth = signInRuleBase.getStartMonth();
        L2RAssert.isTrue(null == startMonth, new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "[startMonth] is required."));
        startMonth = CalendarUtils.getStartTimeOfMonth(startMonth);
        checkSignInRuleBaseStartMonth(lhotelGorupCode, startMonth);
        String title = signInRuleBase.getTitle();
        L2RAssert.stringBlank(description, "[title] is required.");
        L2RAssert.isTrue(signInService.isRuleBaseExit(lhotelGorupCode, null, title), new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "[title] is duplicate."));
        List<SignInAwardBaseDto> signInAwardBases = signInRuleBase.getSignInAwardBases();
        L2RAssert.isTrue(CollectionUtils.isEmpty(signInAwardBases), new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "[signInAwardBases] is required."));
        List<SignInAwardBaseEntity> awardEntities = signInAwardBases.stream()
                .map(signInAwardBaseDto -> generateNewAwardBaseEntity(lhotelGorupCode,signInAwardBaseDto))
                .collect(Collectors.toList());

        String finalSignInRuleBaseCode = signInRuleBaseCode;
        List<SignInAwardMappingEntity> awardMappings = awardEntities.stream()
                .map(awardEntity -> {
                    SignInAwardMappingEntity newAwardMapping = new SignInAwardMappingEntity();
                    newAwardMapping.setLhotelGroupCode(lhotelGorupCode);
                    newAwardMapping.setAwardCode(awardEntity.getCode());
                    newAwardMapping.setRuleBaseCode(finalSignInRuleBaseCode);
                    return newAwardMapping;
                })
                .collect(Collectors.toList());

        SignInRuleBaseEntity newSignInRule = new SignInRuleBaseEntity();
        newSignInRule.setLhotelGroupCode(lhotelGorupCode);
        newSignInRule.setCode(signInRuleBaseCode);
        newSignInRule.setStartMonth(startMonth);
        newSignInRule.setTitle(title);
        newSignInRule.setDescription(description);
        newSignInRule.setIsAutoExtension(DefaultValue.T);
        newSignInRule.setCouldReissue(couldReissue);

        SignInReissueRuleEntity signInReissueShareRuleEntity = null;
        SignInReissueRuleEntity signInReissueResvRuleEntity = null;
        List<SignInReissueRuleEntity> signInReissueRuleEntities = new ArrayList<>();
        List<SignInReissueMappingEntity> signInReissueMappingEntities = new ArrayList<>();
        if (DefaultValue.T.equals(couldReissue)) {

            SignInReissueRuleShareDto signInReissueRuleShare = signInRuleBase.getSignInReissueRuleShare();
            if (null != signInReissueRuleShare) {
                signInReissueShareRuleEntity = new SignInReissueRuleEntity();
                signInReissueShareRuleEntity.setCode(signInService.generateSignInRuleBaseCode(SignInConstant.CodeType.SIGNIN_REISSUE_BASE_CODE));
                signInReissueShareRuleEntity.setName(signInReissueRuleShare.getName());
                signInReissueShareRuleEntity.setType(SignInConstant.ReissueType.SHAER);
                signInReissueShareRuleEntity.setRule(JacksonUtil.beanToJson(signInReissueRuleShare));
                signInReissueShareRuleEntity.setLhotelGroupCode(lhotelGorupCode);
                signInReissueRuleEntities.add(signInReissueShareRuleEntity);

                SignInReissueMappingEntity mappingEntity = new SignInReissueMappingEntity();
                mappingEntity.setReissueCode(signInReissueShareRuleEntity.getCode());
                mappingEntity.setRuleBaseCode(signInRuleBaseCode);
                mappingEntity.setLhotelGroupCode(lhotelGorupCode);
                mappingEntity.setType(SignInConstant.ReissueType.SHAER);
                signInReissueMappingEntities.add(mappingEntity);
            }

            SignInReissueRuleResvDto signInReissueRuleResv = signInRuleBase.getSignInReissueRuleResv();
            if (null != signInReissueRuleResv) {
                signInReissueResvRuleEntity = new SignInReissueRuleEntity();
                signInReissueResvRuleEntity.setRule(JacksonUtil.beanToJson(signInReissueRuleResv));
                signInReissueResvRuleEntity.setCode(signInService.generateSignInRuleBaseCode(SignInConstant.CodeType.SIGNIN_REISSUE_BASE_CODE));
                signInReissueResvRuleEntity.setType(SignInConstant.ReissueType.RESV);
                signInReissueResvRuleEntity.setName(signInReissueRuleResv.getName());
                signInReissueResvRuleEntity.setLhotelGroupCode(lhotelGorupCode);
                signInReissueRuleEntities.add(signInReissueResvRuleEntity);

                SignInReissueMappingEntity mappingEntity = new SignInReissueMappingEntity();
                mappingEntity.setLhotelGroupCode(lhotelGorupCode);
                mappingEntity.setRuleBaseCode(signInRuleBaseCode);
                mappingEntity.setReissueCode(signInReissueResvRuleEntity.getCode());
                mappingEntity.setType(SignInConstant.ReissueType.RESV);
                signInReissueMappingEntities.add(mappingEntity);
            }
        }
        //查询当前 isAutoExtension = "T" 的签到活动

        SignInRuleBaseEntity findInUseSignIn = new SignInRuleBaseEntity();
        findInUseSignIn.setLhotelGroupCode(lhotelGorupCode);
        findInUseSignIn.setIsAutoExtension(DefaultValue.T);
        SignInRuleBaseEntity nowInUseSignIn = commonService.findOnlyOneEQ(SignInRuleBaseEntity.class, findInUseSignIn);
        if (null != nowInUseSignIn) {
            Date nowEndMonth = DateUtil.addMonths(startMonth, -1);
            nowInUseSignIn.setEndMonth(nowEndMonth);
            nowInUseSignIn.setIsAutoExtension(DefaultValue.F);
        }


        signInService.saveSignInRuleBase(newSignInRule, nowInUseSignIn, awardEntities, awardMappings, signInReissueRuleEntities, signInReissueMappingEntities);

    }

    private SignInAwardBaseEntity generateNewAwardBaseEntity(String lhotelGorupCode, SignInAwardBaseDto awardBaseDto) {
        String couldLoopDraw = awardBaseDto.getCouldLoopDraw();
        if (StringUtil.isBlank(couldLoopDraw)) {
            couldLoopDraw = DefaultValue.F;
        }
        Integer dayNum = awardBaseDto.getDayNum();
        L2RAssert.numberNull(dayNum, "[dayNum] is required.");
        String awardDescription = awardBaseDto.getDescription();
//        L2RAssert.stringBlank(awardDescription, "[signInAwardBase.awardDescription] is required.");
        String awardType = awardBaseDto.getType();
        checkAwardType(lhotelGorupCode, awardType);

        String awardCode = signInService.generateSignInRuleBaseCode(SignInConstant.CodeType.SIGNIN_AWARD_BASE_CODE);

        L2RAssert.isTrue(signInService.isAwardBaseExit(lhotelGorupCode, awardCode), new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "[awardCode] is duplicate"));
        String value = awardBaseDto.getValue();
        L2RAssert.stringBlank(value, "[award.value] is required.");
        SignInAwardBaseEntity awardEntity = new SignInAwardBaseEntity();
        awardEntity.setCode(awardCode);
        awardEntity.setDayStep(dayNum);
        awardEntity.setLhotelGroupCode(lhotelGorupCode);
        awardEntity.setDescription(awardDescription);
        awardEntity.setCouldLoopDraw(couldLoopDraw);
        awardEntity.setValue(value);
        awardEntity.setType(awardType);
        return awardEntity;
    }

    private boolean hasNextSignInRuleBase(String lhotelGroupCode) throws Exception {
        Date now = new Date();
        L2RCondition condition = new L2RCondition();
        condition.setAndConditions(
                new ConditionDetail[]{
                        new ConditionDetail(SignInRuleBaseEntity.SignInRuleBaseEntity_.startMonth.toString(), CriteriaUtil.Operator.GT,now),
                        new ConditionDetail(SignInRuleBaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ,lhotelGroupCode),
                }
        );
        long count = commonService.getCountAllByCondition(SignInRuleBaseEntity.class, condition, null);
        return count > 0;
    }
    @Override
    public QueryResult<SignInBaseDto> getSignInBases(SignInRuleBaseFindParamDto paramDto) throws Exception {
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        QueryResult<SignInBaseDto> qres = signInService.getSignInRuleBaseAll(lhotelGroupCode, paramDto.getCurrentPage(), paramDto.getPageSize());
        return qres;
    }

    @Override
    public SignInBaseDto getSignInBase(SignInRuleBaseFindParamDto paramDto) throws Exception {
        String code = paramDto.getCode();
        L2RAssert.stringBlank(code, "[code] is required.");
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is required.");
        return signInService.getSignInBase(lhotelGroupCode, code);
    }

    @Override
    public boolean ruleCodeIsDuplicate(SignInRuleBaseFindParamDto paramDto) throws Exception {
        String code = paramDto.getCode();
        SignInRuleBaseEntity find = new SignInRuleBaseEntity();
        paramDto.setCode(code);
        paramDto.setLhotelGroupCode(paramDto.getLhotelGroupCode());
        long count = commonService.getCountAllEQ(SignInRuleBaseEntity.class, find, null);
        return count > 0;
    }

    @Override
    public QueryResult<SignInAwardTypeDto> awardTypes(BaseL2rParamDto paramDto) throws Exception {
        String isHalt = paramDto.getIsHalt();
        if (StringUtil.isNotBlank(isHalt)) {
            L2RAssert.ishaltVaild(isHalt);
        }
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is required.");
        SignInAwardTypeEntity find = new SignInAwardTypeEntity();
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setIsHalt(isHalt);
        QueryResult<SignInAwardTypeEntity> qres = commonService.findAllEQ(SignInAwardTypeEntity.class, find, paramDto.getCurrentPage(), paramDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(qres, SignInAwardTypeDto.class, true);
    }

    @Override
    public void modifySignInRuleBase(SignInRuleBaseModifyParamDto paramDto) throws Exception {
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        String code = paramDto.getCode();
        L2RAssert.stringBlank(code, "[code] is required.");
        SignInBaseDto signInBase = paramDto.getSignInBase();
        L2RAssert.objectNull(signInBase, "[signInBase] is required");
        L2RAssert.isTrue(signInService.signInRuleIsInUse(lhotelGroupCode,code),new SysException(SIGN_IN_RULE_BASE_MODIFY_ERROR, "签到规则进行中,不能修改."));
        String couldReissue = signInBase.getCouldReissue();
        if (StringUtil.isBlank(couldReissue)) {
            couldReissue = DefaultValue.F;
        }
        SignInRuleBaseEntity find = new SignInRuleBaseEntity();
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setCode(code);
        SignInRuleBaseEntity ruleBase = commonService.findOnlyOneEQ(SignInRuleBaseEntity.class, find);
        L2RAssert.isTrue(null == ruleBase, new SysException(SIGN_IN_RULE_BASE_MODIFY_ERROR, "[code] 活动不存在."));
        Date startMonth = signInBase.getStartMonth();

        L2RAssert.isTrue(null == startMonth, new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "[startMonth] is required."));
        startMonth = CalendarUtils.getStartTimeOfMonth(startMonth);
        Date nowMonth = CalendarUtils.getStartTimeOfMonth(new Date());
        L2RAssert.isTrue(startMonth .compareTo(nowMonth) == 0 || startMonth.before(nowMonth), new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "[startMonth] 必须晚于当前月份"));
        //前一个活动
        SignInRuleBaseEntity preSignInRuleBase = signInService.findPreviousRuleBaseEntity(ruleBase);



        startMonth = CalendarUtils.getStartTimeOfMonth(startMonth);
        String title = signInBase.getTitle();
        L2RAssert.isTrue(2 <= signInService.countRuleBaseExit(lhotelGroupCode, null, title), new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "[title] is duplicate."));
        ruleBase.setStartMonth(startMonth);
        ruleBase.setCouldReissue(couldReissue);
        ruleBase.setDescription(signInBase.getDescription());
        ruleBase.setTitle(title);
        ruleBase.setIsHalt(signInBase.getIsHalt());
        //查询要删除的 award & awardMapping & reissue  & reissueMapping
        SignInAwardMappingEntity findAwardMapping = new SignInAwardMappingEntity();
        findAwardMapping.setLhotelGroupCode(lhotelGroupCode);
        findAwardMapping.setRuleBaseCode(code);
        QueryResult<SignInAwardMappingEntity> awradMappingQres = commonService.findAllEQ(SignInAwardMappingEntity.class, findAwardMapping);
        List<SignInAwardMappingEntity> toDelAwardMapingEntities = awradMappingQres.getResultlist();
        List<SignInAwardBaseEntity> toDelAwards = null;
        if(CollectionUtils.isNotEmpty(toDelAwardMapingEntities)){
            List<String> toDelAwardCodes = toDelAwardMapingEntities.stream()
                    .map(SignInAwardMappingEntity::getAwardCode).collect(Collectors.toList());
            L2RCondition signInAwardCondition = new L2RCondition();
            signInAwardCondition.setAndConditions(
                    new ConditionDetail[]{
                            new ConditionDetail(SignInAwardBaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ,lhotelGroupCode),
                            new ConditionDetail(SignInAwardBaseEntity.SignInAwardBaseEntity_.code.toString(), CriteriaUtil.Operator.IN,toDelAwardCodes),

                    }
            );
            QueryResult<SignInAwardBaseEntity> awards = commonService.findAllByCondition(SignInAwardBaseEntity.class, signInAwardCondition);
            toDelAwards = awards.getResultlist();
        }

        SignInReissueMappingEntity findReissueMapping = new SignInReissueMappingEntity();
        findReissueMapping.setLhotelGroupCode(lhotelGroupCode);
        findReissueMapping.setRuleBaseCode(code);
        QueryResult<SignInReissueMappingEntity> reissueMappingQres = commonService.findAllEQ(SignInReissueMappingEntity.class, findReissueMapping);
        List<SignInReissueMappingEntity> toDelReissuesMapping = reissueMappingQres.getResultlist();
        List<SignInReissueRuleEntity> toDelSignInReissueRuleEntities = null;
        if(CollectionUtils.isNotEmpty(toDelReissuesMapping)){
            List<String> reissueCodes = toDelReissuesMapping.stream().map(SignInReissueMappingEntity::getReissueCode).collect(Collectors.toList());
            L2RCondition signInReissueCondition = new L2RCondition();
            signInReissueCondition.setAndConditions(
                    new ConditionDetail[]{
                            new ConditionDetail(SignInReissueRuleEntity.SignInReissueRuleEntity_.code.toString(), CriteriaUtil.Operator.IN,reissueCodes),
                            new ConditionDetail(SignInReissueRuleEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ,lhotelGroupCode)
                    }
            );
            QueryResult<SignInReissueRuleEntity> signInReissueQres = commonService.findAllByCondition(SignInReissueRuleEntity.class, signInReissueCondition);
            toDelSignInReissueRuleEntities = signInReissueQres.getResultlist();
        }

        //创建新的 奖励规则
        List<SignInAwardBaseDto> signInAwardBases = signInBase.getSignInAwardBases();
        L2RAssert.isTrue(CollectionUtils.isEmpty(signInAwardBases), new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "[signInAwardBases] is required."));
        List<SignInAwardBaseEntity> awardEntities = signInAwardBases.stream()
                .map(signInAwardBaseDto -> generateNewAwardBaseEntity(lhotelGroupCode,signInAwardBaseDto))
                .collect(Collectors.toList());


        String finalSignInRuleBaseCode = code;
        List<SignInAwardMappingEntity> awardMappings = awardEntities.stream()
                .map(awardEntity -> {
                    SignInAwardMappingEntity newAwardMapping = new SignInAwardMappingEntity();
                    newAwardMapping.setLhotelGroupCode(lhotelGroupCode);
                    newAwardMapping.setAwardCode(awardEntity.getCode());
                    newAwardMapping.setRuleBaseCode(finalSignInRuleBaseCode);
                    return newAwardMapping;
                })
                .collect(Collectors.toList());

        List<SignInReissueRuleEntity> signInReissueRuleEntities = new ArrayList<>();
        List<SignInReissueMappingEntity> signInReissueMappingEntities = new ArrayList<>();
        if (DefaultValue.T.equals(couldReissue)) {

            SignInReissueRuleShareDto signInReissueRuleShare = signInBase.getSignInReissueRuleShare();
            if (null != signInReissueRuleShare) {
                SignInReissueRuleEntity signInReissueShareRuleEntity = new SignInReissueRuleEntity();
                signInReissueShareRuleEntity.setCode(signInService.generateSignInRuleBaseCode(SignInConstant.CodeType.SIGNIN_REISSUE_BASE_CODE));
                signInReissueShareRuleEntity.setName(signInReissueRuleShare.getName());
                signInReissueShareRuleEntity.setType(SignInConstant.ReissueType.SHAER);
                signInReissueShareRuleEntity.setRule(JacksonUtil.beanToJson(signInReissueRuleShare));
                signInReissueShareRuleEntity.setLhotelGroupCode(lhotelGroupCode);
                signInReissueRuleEntities.add(signInReissueShareRuleEntity);

                SignInReissueMappingEntity mappingEntity = new SignInReissueMappingEntity();
                mappingEntity.setReissueCode(signInReissueShareRuleEntity.getCode());
                mappingEntity.setRuleBaseCode(code);
                mappingEntity.setLhotelGroupCode(lhotelGroupCode);
                mappingEntity.setType(SignInConstant.ReissueType.SHAER);
                signInReissueMappingEntities.add(mappingEntity);
            }

            SignInReissueRuleResvDto signInReissueRuleResv = signInBase.getSignInReissueRuleResv();
            if (null != signInReissueRuleResv) {
                SignInReissueRuleEntity signInReissueResvRuleEntity = new SignInReissueRuleEntity();
                signInReissueResvRuleEntity.setRule(JacksonUtil.beanToJson(signInReissueRuleResv));
                signInReissueResvRuleEntity.setCode(signInService.generateSignInRuleBaseCode(SignInConstant.CodeType.SIGNIN_REISSUE_BASE_CODE));
                signInReissueResvRuleEntity.setType(SignInConstant.ReissueType.RESV);
                signInReissueResvRuleEntity.setName(signInReissueRuleResv.getName());
                signInReissueResvRuleEntity.setLhotelGroupCode(lhotelGroupCode);
                signInReissueRuleEntities.add(signInReissueResvRuleEntity);

                SignInReissueMappingEntity mappingEntity = new SignInReissueMappingEntity();
                mappingEntity.setLhotelGroupCode(lhotelGroupCode);
                mappingEntity.setRuleBaseCode(code);
                mappingEntity.setReissueCode(signInReissueResvRuleEntity.getCode());
                mappingEntity.setType(SignInConstant.ReissueType.RESV);
                signInReissueMappingEntities.add(mappingEntity);
            }
        }

        List<SignInRuleBaseEntity> signInRuleBasesToModify = new ArrayList<>();
        signInRuleBasesToModify.add(ruleBase);
        if(null != preSignInRuleBase ){
            Date nowEndMonth = DateUtil.addMonths(startMonth, -1);
            preSignInRuleBase.setEndMonth(nowEndMonth);
            signInRuleBasesToModify.add(preSignInRuleBase);
        }
        ISignInService.ModifySignInRule modifySignInRule = new ISignInService.ModifySignInRule();
        modifySignInRule.setDelSignInAwardBases(toDelAwards);
        modifySignInRule.setDelSignInAwardMappingEntities(toDelAwardMapingEntities);
        modifySignInRule.setDelSignInReissueRuleEntities(toDelSignInReissueRuleEntities);
        modifySignInRule.setDelSignInReissueMappingEntities(toDelReissuesMapping);
        modifySignInRule.setNewSignInAwardBases(awardEntities);
        modifySignInRule.setNewSignInAwardMappingEntities(awardMappings);
        modifySignInRule.setNewSignInReissueMappingEntities(signInReissueMappingEntities);
        modifySignInRule.setNewSignInReissueRuleEntities(signInReissueRuleEntities);
        signInService.modifySignInRule(modifySignInRule);
    }

    private void checkAwardType(String lhotelGroupCode, String type) {
        L2RAssert.stringBlank(type, "[signInAwardBase.type] is required.");
        try {
            SignInAwardTypeEntity entity = signInService.findAwardType(lhotelGroupCode, type);
            L2RAssert.isTrue(null == entity, new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "[award.type] is not exit"));
        } catch (Exception e) {
            throw new SysException(BaseSysExceptionEnum.BASE_FUCATION_OPTION, e);
        }
    }

    private void checkSignInRuleBaseStartMonth(String lhotelGroupCode, Date startMonth) throws Exception {
        Date now = CalendarUtils.getStartTimeOfMonth(new Date());
        boolean hasUnderway = signInService.hasUnderwaySignBase(lhotelGroupCode);
        if(hasUnderway){
            L2RAssert.isTrue(startMonth.before(DateUtil.addMonths(now, 1)), new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "开始时间请从次月开始."));
        }
        L2RCondition condition = new L2RCondition();
        condition.setAndConditions(
                new ConditionDetail[]{
                        new ConditionDetail(SignInRuleBaseEntity.SignInRuleBaseEntity_.startMonth.toString(), CriteriaUtil.Operator.GTE, startMonth),
                        new ConditionDetail(SignInRuleBaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ, lhotelGroupCode)
                }
        );
        long count = commonService.getCountAllByCondition(SignInRuleBaseEntity.class, condition, null);
        L2RAssert.isTrue(count > 0, new SysException(SIGN_IN_RULE_BASE_CREATE_ERROR, "新建活动月份必须晚于现有活动月份"));
    }
}
